﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.utils.*;
    import __AS3__.vec.*;

	/**
	 * 对比SavedObject多了组件列表和事件回调函数的列表
	 */
    public class EngineObject extends SavedObject implements IEventDispatcher {

        protected var m_components:Dictionary;
        protected var m_events:Dictionary;

        public function EngineObject(){
            this.m_components = new Dictionary();
            this.m_events = new Dictionary();
            super();
            this.addComponents();
            this.initializeComponents();
        }
        override public function getSaveObject():Object{
            var _local3:Object;
            var _local4:EngineObjectComponent;
            var _local1:Object = super.getSaveObject();
            var _local2:Object = new Object();
            for each (_local4 in this.m_components) {
                _local3 = _local4.getSaveObject();
                if (_local3){
                    _local2[_local4.name] = _local3;
                };
            };
            _local1.components = _local2;
            return (_local1);
        }
        override public function loadObject(_arg1:Object):void{
            var _local3:EngineObjectComponent;
            super.loadObject(_arg1);
            var _local2:Object = _arg1.components;
            if (_local2 != null){
                for each (_local3 in this.m_components) {
                    if (_local2.hasOwnProperty(_local3.name)){
                        _local3.loadObject(_local2[_local3.name]);
                    };
                };
            };
        }
        protected function addComponents():void{
        }
        final protected function addComponent(_arg1:EngineObjectComponent):Boolean{
            var _local2:Boolean;
            if (!_arg1.owner){
                if (!this.m_components.hasOwnProperty(_arg1.name)){
                    this.m_components[_arg1.name] = _arg1;
                    _arg1.register(this);
                    _local2 = true;
                } else {
                    ErrorManager.addError((("A engine object component with name " + _arg1.name) + " already exists on this engine object."));
                };
            } else {
                ErrorManager.addError((("The engine object component " + _arg1.name) + " already has an owner."));
            };
            return (_local2);
        }
        final protected function removeComponent(_arg1:EngineObjectComponent):void{
            if (this == _arg1.owner){
                if (this.m_components[_arg1.name]){
                    delete this.m_components[_arg1.name];
                    _arg1.unregister();
                } else {
                    ErrorManager.addError((("The engine object component " + _arg1.name) + " was not found on this engine object."));
                };
            } else {
                ErrorManager.addError((("The engine object component " + _arg1.name) + " is not owned by this engine object."));
            };
        }
        final protected function initializeComponents():void{
            var _local1:EngineObjectComponent;
            for each (_local1 in this.m_components) {
                _local1.initializeComponent();
            };
        }
        public function get components():Dictionary{
            return (this.m_components);
        }
        public function lookupComponentByName(_arg1:String):EngineObjectComponent{
            var _local2:EngineObjectComponent;
            if (this.m_components[_arg1]){
                _local2 = this.m_components[_arg1];
            };
            return (_local2);
        }
        public function onUpdate(_arg1:Number):void{
            this.dispatchEvent(new UpdateEvent(UpdateEvent.UPDATE, _arg1));
        }
        public function onMouseOver(_arg1:MouseEvent):void{
            this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
        }
        public function onMouseOut():void{
            this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
        }
        public function onClick():void{
            this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
        public function getProperty(_arg1:PropertyReference, _arg2=null){
            var component:* = null;
            var reference:* = _arg1;
            var defaultVal = _arg2;
            var result:* = null;
            if (((((!((null == reference))) && (!((null == reference.property))))) && (!(("" == reference.property))))){
                try {
                    component = this.lookupComponentByName(reference.propertyParent);
                    result = component[reference.propertyName];
                } catch(error:Error) {
                    result = null;
                };
            };
            if (null == result){
                result = defaultVal;
            };
            return (result);
        }
        public function setProperty(_arg1:PropertyReference, _arg2):void{
            var component:* = null;
            var reference:* = _arg1;
            var value:* = _arg2;
            if (((((!((null == reference))) && (!((null == reference.property))))) && (!(("" == reference.property))))){
                try {
                    component = this.lookupComponentByName(reference.propertyParent);
                    component[reference.propertyName] = value;
                } catch(error:Error) {
                };
            };
        }
        public function callFunction(_arg1:FunctionReference, ... _args){
            var component:* = null;
            var reference:* = _arg1;
            var args:* = _args;
            var result:* = null;
            if (((((!((null == reference))) && (!((null == reference.functionPath))))) && (!(("" == reference.functionPath))))){
                try {
                    component = this.lookupComponentByName(reference.functionParent);
                    if (0 == args.length){
                        result = (component[reference.functionName] as Function)();
                    } else {
                        if (1 == args.length){
                            result = (component[reference.functionName] as Function)(args[0]);
                        } else {
                            result = (component[reference.functionName] as Function).apply(component, args);
                        };
                    };
                } catch(error:Error) {
                };
            };
            return (result);
        }
        public function addEventListener(_arg1:String, _arg2:Function, _arg3:Boolean=false, _arg4:int=0, _arg5:Boolean=false):void{
            if (this.m_events.hasOwnProperty(_arg1) == false){
                this.m_events[_arg1] = new Vector.<Function>();
            };
            (this.m_events[_arg1] as Vector.<Function>).push(_arg2);
        }
        public function dispatchEvent(_arg1:Event):Boolean{
            var _local3:Vector.<Function>;
            var _local4:uint;
            var _local5:uint;
            var _local2:Boolean;
            if (this.m_events.hasOwnProperty(_arg1.type) == true){
                _local3 = (this.m_events[_arg1.type] as Vector.<Function>);
                _local4 = _local3.length;
                _local5 = 0;
                while (_local5 < _local4) {
                    var _local6 = _local3;
                    _local6[_local5](_arg1);
                    _local5++;
                };
                _local2 = true;
            };
            return (_local2);
        }
        public function hasEventListener(_arg1:String):Boolean{
            return (this.m_events.hasOwnProperty(_arg1));
        }
        public function removeEventListener(_arg1:String, _arg2:Function, _arg3:Boolean=false):void{
            var _local4:Vector.<Function>;
            var _local5:uint;
            var _local6:uint;
            if (this.m_events.hasOwnProperty(_arg1) == true){
                _local4 = (this.m_events[_arg1] as Vector.<Function>);
                _local5 = _local4.length;
                _local6 = 0;
                while (_local6 < _local5) {
                    if (_local4[_local6] == _arg2){
                        _local4 = _local4.splice(_local6, 1);
                        break;
                    };
                    _local6++;
                };
            };
        }
        public function willTrigger(_arg1:String):Boolean{
            GlobalEngine.error("EngineObject", "willTrigger() function not currently supported");
            return (false);
        }

    }
}//package Engine.Classes 
